package com.kkd.kkdapp.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.LivePlayerActivity;
import com.kkd.kkdapp.adapter.LiveHomeGridAdapter.ViewHolder;
import com.kkd.kkdapp.http.entity.LiveHomeDataLive;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class LiveDetailGridAdapter extends BaseAdapter {
	private Context mContext;
	private List<LiveHomeDataLive> mList;
	private LayoutInflater inflater;
	private OnLiveHomeDataLiveClick mOnLiveHomeDataLiveClick;

	public LiveDetailGridAdapter(Context context, List<LiveHomeDataLive> list,OnLiveHomeDataLiveClick onLiveHomeDataLiveClick) {
		mContext = context;
		mList = list;
		inflater = LayoutInflater.from(mContext);	
		mOnLiveHomeDataLiveClick = onLiveHomeDataLiveClick;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_gridview_livehome,
					parent, false);
			holder.iv_logo = (ImageView) convertView.findViewById(R.id.iv_logo);
			holder.iv_content = (ImageView) convertView
					.findViewById(R.id.iv_content);
			holder.tv_num_played = (TextView) convertView
					.findViewById(R.id.tv_num_played);
			holder.tv_title = (TextView) convertView
					.findViewById(R.id.tv_title);
			holder.toDetail = convertView.findViewById(R.id.toDetail);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.iv_logo.setVisibility(View.GONE);
		holder.tv_num_played.setVisibility(View.GONE);
		holder.iv_content.setImageResource(R.drawable.live_loading);
		ImageLoader.getInstance().displayImage(
				mList.get(position).getCoverUrl(), holder.iv_content,
				new ImageLoadingListener() {

					@Override
					public void onLoadingStarted(String imageUri, View view) {

					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {

					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						holder.iv_logo.setVisibility(View.VISIBLE);
						holder.tv_num_played.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadingCancelled(String imageUri, View view) {

					}
				});

		holder.tv_title.setText(mList.get(position).getVideoName());
		holder.tv_num_played.setText("回放" + mList.get(position).getPlayCount()
				+ "次");
		// 点击跳转到详情
		holder.toDetail.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				if(mList!=null&&position<mList.size()){
					mOnLiveHomeDataLiveClick.OnLiveHomeDataLiveClick(mList.get(position));
				}
				
				
			}
		});
		return convertView;
	}

	class ViewHolder {
		ImageView iv_logo, iv_content;
		TextView tv_num_played, tv_title;
		View toDetail;
	}
	
	public interface OnLiveHomeDataLiveClick{
		public void OnLiveHomeDataLiveClick(LiveHomeDataLive ld);
	}
}
